package com.xiaoxie.web;

import com.xiaoxie.common.redis.RedisManager;
import com.xiaoxie.common.union.R;
import com.xiaoxie.common.utils.HttpClientUtil;
import com.xiaoxie.system.domain.Ds;
import jakarta.annotation.Resource;
import org.apache.hc.core5.http.ParseException;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class TestController {

    @Resource
    private RedisManager redisManager;

    @Resource
    private HttpClientUtil httpClientUtil;

    @GetMapping("/redisManager")
    public String redisManager() {
        redisManager.setValue("test", "test");
        return redisManager.getValue("test").toString();
    }

    @GetMapping("/httpClient")
    public String testHttpClient() throws IOException, ParseException {
        return httpClientUtil.sendGet("http://localhost:8080/system/logout", null, null);
    }

    @PutMapping("/testBean")
    public TestBean testHttpClient1(@RequestBody TestBean tb) throws IOException, ParseException {
         System.out.println("name:"+tb.getName()+",age:"+tb.getAge());
         return tb;
    }

    @GetMapping("/httpClient2")
    public R testHttpClient2() throws IOException, ParseException {
        String body = "{\"name\":\"value1\", \"age\":18}";
        Map<String,String> params = new HashMap<>();
        params.put("name","value1");
        params.put("age","18");
        return httpClientUtil.sendPut("http://localhost:8080/test/testBean", body, null,TestBean.class);
    }

    @GetMapping("/httpClient3")
    public R testHttpClient3() throws IOException, ParseException {
        Map<String,String> headers = new HashMap<>();
        headers.put("satoken","9e2066b6-03cb-4c1d-a7f7-304cf4d2b0eb");

        return httpClientUtil.sendGet("http://localhost:8080/system/ds/list", null, headers, List.of("data","list"));
    }

}
